package com.sdry.web.controller.cb;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sdry.model.cb.ReportFormEntity;
import com.sdry.model.cb.TableDataInfo;
import com.sdry.model.jyy.Receive;
import com.sdry.model.llm.LlmCancellingStocks;
import com.sdry.model.lz.*;
import com.sdry.model.zc.ZcInventoryInfoEntity;
import com.sdry.model.zc.ZcRejectsWarehouseEntity;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.cb.ReportFormService;
import com.sdry.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.*;
import java.util.List;

/**
 * 报表模块
 * @ClassName
 * @Description
 * @Author cb
 * @Date 2019年8月26日
 * @Version 1.0
 */
@Controller
@RequestMapping("/reportForm")
public class ReportFormController extends BaseController{


    @Autowired
    private ReportFormService reportFormService;

    @RequestMapping("/geLiPage")
    public String geLiPage(Model model) {
        //查询所有供应商
        List<Customer> allCustomerList = reportFormService.selectAllCustomerList();
        model.addAttribute("allCustomerList",allCustomerList);
        return "bb/geLi";
    }

    @RequestMapping("/fanGongPage")
    public String fanGongPage(Model model) {
        //查询所有供应商
        List<Customer> allCustomerList = reportFormService.selectAllCustomerList();
        model.addAttribute("allCustomerList",allCustomerList);

        return "bb/fanGong";
    }

    @RequestMapping("/returnPage")
    public String returnPage(Model model) {
        //查询所有供应商
        List<Customer> allCustomerList = reportFormService.selectAllCustomerList();
        model.addAttribute("allCustomerList",allCustomerList);
        return "bb/return";
    }


    /**
     * 月报表,多仓库版本 cb_2019年11月7日20:41:56
     * @return
     */
    @RequestMapping(value = "getReportFormList")
    @ResponseBody
    public TableDataInfo getReportFormList(
            String selectTime2, Integer page , Integer pageSize, String ma_id, String date,Long customer_id
    ) {
        try {
            logger.info("获取月报表列表,多仓库版本 cb_2019年11月7日20:41:56 customer_id="+customer_id+",pageSize="+pageSize+",ma_id="+ma_id+",date="+date+"customer_id"+customer_id);
            if (date.length() == 7)date += "-01";
            String firsTime = reportFormService.getReportFormFirsTime();
            List<ReportFormEntity> list = reportFormService.getReportFormList(ma_id,date,customer_id,firsTime,selectTime2,page,pageSize);
            Integer count = reportFormService.getReportFormListCount(ma_id,date,customer_id,firsTime,selectTime2);
            return getDataTable(list,count);
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("cb_获取月报表列表出错,");
            return getDataTable(new ArrayList<>(),100);
        }


    }
    /**
     * 导出Excel
     * @param response
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("/ExportallReportFormList")
    public void ExportallReportFormList(HttpServletResponse response , String selectTime2, Integer page , Integer pageSize, String ma_id, String date,Long customer_id) throws UnsupportedEncodingException, ParseException{
        String firsTime = reportFormService.getReportFormFirsTime();
        List<ReportFormEntity> list = reportFormService.getReportFormList(ma_id,date,customer_id,firsTime,selectTime2,1,10000);
        List<List<String>> lists = zhuanlisty(list);
        ExcelUtils.export("月报表所有数据",namesy(), response, lists);
    }

    public List<String> namesy() {
        List<String> list = new ArrayList<>();
        list.add("序号");
        list.add("供应商");
        list.add("SAP/QAD");
        list.add("零件名称");
        list.add("单包装");

        list.add("上月隔离结存");
        list.add("上月结存");
        list.add("本月累计入库");
        list.add("本月累计发出");
        list.add("退货");
        list.add("本月结存");
        list.add("本月累计不良");
        list.add("本月累计返工");
        list.add("不良结存");
        list.add("当月可用库存");

        return list;
    }

    /**
     * 根据导出数据表头把List<cuser>转List<List<String>>
     * @param materiels
     * @return
     */
    public List<List<String>> zhuanlisty (List<ReportFormEntity> ReportFormEntitys){
        int count = 0;
        List<List<String>> lists = new ArrayList<>();

        for (ReportFormEntity reportFormEntity : ReportFormEntitys) {
            List<String> list = new ArrayList<>();
            ++count;
            list.add(String.valueOf(count));
            list.add(reportFormEntity.getCustomerName());
            list.add(reportFormEntity.getMaterielNum());
            list.add(reportFormEntity.getMaterielName());

            list.add(String.valueOf(reportFormEntity.getPackingQuantity()));


            if(reportFormEntity.getLastGeLiBalance()==null) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getLastGeLiBalance()));
            }
            if(reportFormEntity.getLastMonthBalance()==null) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getLastMonthBalance()));
            }
            if(reportFormEntity.getThisMonthAdd()==null||reportFormEntity.getThisMonthAdd()==0) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getThisMonthAdd()));
            }

            if(reportFormEntity.getThisMonthReduce()==null||reportFormEntity.getThisMonthReduce()==0) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getThisMonthReduce()));
            }


            if(reportFormEntity.getThisMonthReturn()==null||reportFormEntity.getThisMonthReturn()==0) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getThisMonthReturn()));
            }
            if(reportFormEntity.getThisMonthBalance()==null||reportFormEntity.getThisMonthBalance()==0) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getThisMonthBalance()));
            }


            if(reportFormEntity.getGeLiBalance()==null) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getGeLiBalance()));
            }
            if(reportFormEntity.getGetFangong()==null) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getGetFangong()));
            }
            list.add(String.valueOf(Integer.valueOf(reportFormEntity.getGeLiBalance())-Integer.valueOf(reportFormEntity.getGetFangong())));
            list.add(String.valueOf(Integer.valueOf(reportFormEntity.getThisMonthBalance())-Integer.valueOf(reportFormEntity.getGeLiBalance())+Integer.valueOf(reportFormEntity.getGetFangong())));
            lists.add(list);
            list = null;
        }
        return lists;
    }

    /**
     * 日报表,多仓库版本 cb_2019年11月7日20:41:56
     * @return
     */
    @RequestMapping(value = "getDateBalance")
    @ResponseBody
    public TableDataInfo getDateBalance(
            String dateTime2 , Integer page , Integer pageSize, String ma_id, String date,Long customer_id
    )  {

        try {
            logger.info("获取日报表列表,多仓库版本 cb_2019年11月7日20:41:56");
            String firsTime = reportFormService.getReportFormFirsTime();
            List<ReportFormEntity> list = reportFormService.getDateBalance(ma_id, date, customer_id, firsTime, dateTime2,page,pageSize);
            return getDataTable(list,reportFormService.getDateBalanceCount(ma_id, date, customer_id, firsTime, dateTime2));
        } catch (ParseException e) {
            e.printStackTrace();
            logger.error("获取日报表列表报错");
            return getDataTable(new ArrayList<>(),100);
        }
    }

    /**
     * 导出Excel
     * @param response
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("/ExportallDateBalance")
    public void ExportallDateBalance(HttpServletResponse response , String dateTime2 , Integer page , Integer pageSize, String ma_id, String date,Long customer_id) throws UnsupportedEncodingException, ParseException{
        String firsTime = reportFormService.getReportFormFirsTime();
        page = 1;pageSize = 10000;
        List<ReportFormEntity> list = reportFormService.getDateBalance(ma_id, date, customer_id, firsTime, dateTime2, page, pageSize);
        List<List<String>> lists = zhuanlist(list);
        ExcelUtils.export("日报表所有数据",namess(), response, lists);
    }

    public static List<String> namess() {
        List<String> list = new ArrayList<>();
        list.add("序号");
        list.add("供应商");
        list.add("时间");
        list.add("零件名称");
        list.add("SAP/QAD");
        list.add("昨日结存");
        list.add("当日入库");
        list.add("当日出库");
        list.add("当日退库");
        list.add("当日不良数");
        list.add("库存总量");
        list.add("良品数量");
        //list.add("差异");
        //list.add("上限");
        //list.add("下限");
        return list;
    }

    /**
     * 根据导出数据表头把List<cuser>转List<List<String>>
     * @param materiels
     * @return
     */
    public static List<List<String>> zhuanlist (List<ReportFormEntity> ReportFormEntitys){
        int count = 0;
        List<List<String>> lists = new ArrayList<>();
        for (ReportFormEntity reportFormEntity : ReportFormEntitys) {
            List<String> list = new ArrayList<>();
            ++count;
            list.add(String.valueOf(count));
            list.add(reportFormEntity.getCustomerName());
            list.add(reportFormEntity.getCreateDate());
            list.add(reportFormEntity.getMaterielName());

            list.add(reportFormEntity.getMaterielNum());
            list.add(String.valueOf(reportFormEntity.getYestdayNum()));

            if(reportFormEntity.getThisMonthAdd()==null||reportFormEntity.getThisMonthAdd()==0) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getThisMonthAdd()));
            }

            if(reportFormEntity.getThisMonthReduce()==null||reportFormEntity.getThisMonthReduce()==0) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getThisMonthReduce()));
            }

            //返厂

            if(reportFormEntity.getThisMonthReturn()==null||reportFormEntity.getThisMonthReturn()==0) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getThisMonthReturn()));
            }

            if(reportFormEntity.getGeLiBalance()==null) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getGeLiBalance()));
            }

            if(reportFormEntity.getGeLiBalance()==null) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getThisMonthAdd()-reportFormEntity.getThisMonthReduce()+reportFormEntity.getYestdayNum()-reportFormEntity.getThisMonthReturn()));
            }

            if(reportFormEntity.getGeLiBalance()==null) {
                list.add(String.valueOf(0));
            }else {
                list.add(String.valueOf(reportFormEntity.getThisMonthAdd()-reportFormEntity.getThisMonthReduce()+reportFormEntity.getYestdayNum()-Integer.valueOf(reportFormEntity.getGeLiBalance())));
            }

            lists.add(list);
            list = null;
        }
        return lists;
    }

    /**
     * 入库报表,多仓库版本 cb_2019年11月7日20:41:56
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "getReportFormJinList")
    @ResponseBody
    public TableDataInfo getReportFormJinList(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2
    ) {
        try {
            logger.info("获取入库报表列表,多仓库版本 cb_2019年11月7日20:41:56,selectTime2="+selectTime2+",customer_id="+customer_id+",ma_id="+maName+",date="+date+"selectTime2="+selectTime2);
            List<Receive> list = reportFormService.getReportFormJinList(selectTime,maName,date,customer_id,selectType,selectTime2,page,pageSize);
            Integer count = reportFormService.getReportFormJinListTotal(selectTime,maName,date,customer_id,selectType,selectTime2);
            return getDataTable(list, count);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取入库报表列表出错");
            return getDataTable(new ArrayList<>(),100);
        }
    }

    /**
     * 导出Excel
     * @param response
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("/ExportReportFormJinList")
    public void ExportReportFormJinList(HttpServletResponse response ,String selectType,
                                        String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2) throws UnsupportedEncodingException, ParseException{
        List<Receive> list = reportFormService.getReportFormJinList(selectTime,maName,date,customer_id,selectType,selectTime2, 1, 1000);
        List<List<String>> lists = zhuanlistr(list);
        ExcelUtils.export("入库报表所有数据",namessr(), response, lists);
    }

    public List<String> namessr() {
        List<String> list = new ArrayList<>();
        list.add("序号");
        list.add("供应商");
        list.add("类型");
        list.add("时间");
        list.add("单号");
        list.add("QAD");
        list.add("物料名称");
        list.add("实际数量");

        return list;
    }

    /**
     * 根据导出数据表头把List<cuser>转List<List<String>>
     * @param
     * @return
     */
    public List<List<String>> zhuanlistr (List<Receive> Receives){
        int count = 0;
        List<List<String>> lists = new ArrayList<>();
        for (Receive receive : Receives) {
            List<String> list = new ArrayList<>();
            ++count;
            list.add(String.valueOf(count));
            list.add(receive.getSendPhone());
            if(receive.getReceive_company().equals("1")) {
                list.add("精简入库");
            }else {
                list.add("一般入库");
            }
            list.add(receive.getCreateDate());
            list.add(receive.getReceiveNumber());
            list.add(receive.getSendName());
            list.add(receive.getSendCompany());
            list.add(receive.getSendDate());

            lists.add(list);
            list = null;
        }
        return lists;
    }

    /**
     * 出库报表,多仓库版本 cb_2019年11月7日20:41:56
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "getReportFormChungList")
    @ResponseBody
    public TableDataInfo getReportFormChungList(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2
    ) {
        try {
            logger.info("获取出库报表列表,多仓库版本 cb_2019年11月7日20:41:56,selectTime2="+selectTime2+",customer_id="+customer_id+",ma_id="+maName+",date="+date+"selectTime2="+selectTime2);
            List<OutStockOrder> list = reportFormService.getReportFormChungList(maName,date,customer_id,selectTime,selectType,selectTime2,page,pageSize);
            Integer total = reportFormService.getReportFormChungListTotal(maName,date,customer_id,selectTime,selectType,selectTime2);
            return getDataTable(list, total);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取出库报表列表出错");
            return getDataTable(new ArrayList<>(),100);
        }
    }
    /**
     * 导出Excel
     * @param response
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("/ExportReportFormChungList")
    public void ExportReportFormChungList(HttpServletResponse response ,String selectType,
                                          String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2) throws UnsupportedEncodingException, ParseException{
        List<OutStockOrder> list = reportFormService.getReportFormChungList(maName,date,customer_id,selectTime,selectType,selectTime2, 1, 10000);
        List<List<String>> lists = zhuanlistc(list);
        ExcelUtils.export("出库报表所有数据",namessc(), response, lists);
    }

    public List<String> namessc() {
        List<String> list = new ArrayList<>();
        list.add("序号");
        list.add("供应商");
        list.add("时间");
        list.add("类型");

        list.add("单号");
        list.add("QAD");
        list.add("物料名称");
        list.add("实际数量");

        return list;
    }

    /**
     * 根据导出数据表头把List<cuser>转List<List<String>>
     * @param materiels
     * @return
     */
    public List<List<String>> zhuanlistc (List<OutStockOrder> OutStockOrders){
        int count = 0;
        List<List<String>> lists = new ArrayList<>();
        for (OutStockOrder outStockOrder : OutStockOrders) {
            List<String> list = new ArrayList<>();
            ++count;
            list.add(String.valueOf(count));
            list.add(outStockOrder.getContacts_tel());
            list.add(outStockOrder.getCreate_date());
            if(outStockOrder.getImg_url().equals("0")) {
                list.add("精简出库");
            }else if(outStockOrder.getImg_url().equals("2")) {
                list.add("紧急出库");
            }else {
                list.add("一般出库");
            }
            list.add(outStockOrder.getOut_stock_num());
            list.add(outStockOrder.getWarehouse_address());
            list.add(outStockOrder.getWarehouse_name());
            list.add(outStockOrder.getUserName());

            lists.add(list);
            list = null;
        }
        return lists;
    }

    /**
     * 退库报表,多仓库版本  cb_2019年11月7日19:07:43
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "getReportFormReTurnList")
    @ResponseBody
    public TableDataInfo getReportFormReTurnList(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String dateTime2
    ) {

        try {
            logger.info("获取退库报表列表，多仓库版本  cb_2019年11月7日19:07:43,+dateTime2="+dateTime2+",customer_id="+customer_id+ ",ma_id="+maName+",date="+date);
            List<LlmCancellingStocks> list = reportFormService.getReportFormReTurnList(selectTime,maName,date,customer_id,selectType,dateTime2,page,pageSize);
            Integer total = reportFormService.getReportFormReTurnListTotal(selectTime,maName,date,customer_id,selectType,dateTime2);
            return getDataTable(list, total);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取退库报表列表出错");
            return getDataTable(new ArrayList<>(),100);
        }
    }
    /**
     * 导出Excel
     * @param response
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("/ExportReportFormReTurnList")
    public void ExportReportFormReTurnList(HttpServletResponse response , String selectType,
                                           String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String dateTime2) throws UnsupportedEncodingException, ParseException{
        String firsTime = reportFormService.getReportFormFirsTime();
        List<LlmCancellingStocks> list =reportFormService.getReportFormReTurnList(selectTime,maName,date,customer_id,selectType,dateTime2, 1, 10000);
        List<List<String>> lists = zhuanlistt(list);
        ExcelUtils.export("退库报表所有数据",namesst(), response, lists);
    }

    public List<String> namesst() {
        List<String> list = new ArrayList<>();
        list.add("序号");
        list.add("供应商");
        list.add("时间");
        list.add("类型");

        list.add("单号");
        list.add("QAD");
        list.add("物料名称");
        list.add("实际数量");

        return list;
    }

    /**
     * 根据导出数据表头把List<cuser>转List<List<String>>
     * @param materiels
     * @return
     */
    public List<List<String>> zhuanlistt (List<LlmCancellingStocks> LlmCancellingStockss){
        int count = 0;
        List<List<String>> lists = new ArrayList<>();
        for (LlmCancellingStocks cancellingStocks : LlmCancellingStockss) {
            List<String> list = new ArrayList<>();
            ++count;
            list.add(String.valueOf(count));
            list.add(cancellingStocks.getKeyword03());
            list.add(cancellingStocks.getCreateDate());

            list.add("退货");
            list.add(cancellingStocks.getCancellingNumber());
            list.add(cancellingStocks.getPhone());
            list.add(cancellingStocks.getUserName());
            list.add(cancellingStocks.getContacts_name());

            lists.add(list);
            list = null;
        }
        return lists;
    }



    /**
     * 不良库报表,多仓库版本cb-2019年11月7日20:26:12
     * @param page 当前页
     * @param pageSize 每页大小
     * @param maName 物料名称
     * @param date 日期
     * @param customer_id 供应商
     */
    @RequestMapping(value = "getReportFormReGeLIList")
    @ResponseBody
    public TableDataInfo getReportFormReGeLIList(
            String selectType,
            String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2
    )  {
        try {
            logger.info("获取不良库报表列表,多仓库版本cb-2019年11月7日20:26:12,selectTime2="+customer_id+",customer_id="+pageSize+ ",ma_id="+maName+",date="+date);
            List<ZcRejectsWarehouseEntity> list = reportFormService.getReportFormReGeLIList(selectTime,maName,date,customer_id,selectType,selectTime2,page,pageSize);
            Integer total = reportFormService.getReportFormReGeLIListTotal(selectTime,maName,date,customer_id,selectType,selectTime2);
            return getDataTable(list, total);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取不良库报表列表出错");
            return getDataTable(new ArrayList<>(),100);
        }
    }
    /**
     * 导出Excel
     * @param response
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("/ExportReportFormReGeLIList")
    public void ExportReportFormReGeLIList(HttpServletResponse response ,String selectType,
                                           String selectTime, Integer page , Integer pageSize, String maName, String date,Long customer_id,String selectTime2) throws UnsupportedEncodingException, ParseException{
        List<ZcRejectsWarehouseEntity> list = reportFormService.getReportFormReGeLIList(selectTime,maName,date,customer_id,selectType,selectTime2, 1, 100000);
        List<List<String>> lists = zhuanlistg(list);
        ExcelUtils.export("隔离报表所有数据",namessg(), response, lists);
    }

    public List<String> namessg() {
        List<String> list = new ArrayList<>();
        list.add("序号");
        list.add("供应商");
        list.add("时间");
        list.add("类型");

        list.add("批次号（单号）");
        list.add("QAD");
        list.add("物料名称");
        list.add("实际数量");

        return list;
    }

    /**
     * 根据导出数据表头把List<cuser>转List<List<String>>
     * @param materiels
     * @return
     */
    public List<List<String>> zhuanlistg (List<ZcRejectsWarehouseEntity> zcRejectsWarehouseEntitys){
        int count = 0;
        List<List<String>> lists = new ArrayList<>();
        for (ZcRejectsWarehouseEntity zcRejectsWarehouse : zcRejectsWarehouseEntitys) {
            List<String> list = new ArrayList<>();
            ++count;
            list.add(String.valueOf(count));
            list.add(zcRejectsWarehouse.getMateriel_properties());
            list.add(zcRejectsWarehouse.getEnterDate());

            list.add("隔离");
            list.add(zcRejectsWarehouse.getmBatch());
            list.add(zcRejectsWarehouse.getRegion());
            list.add(zcRejectsWarehouse.getWarehouse());
            list.add(String.valueOf(zcRejectsWarehouse.getTotality()));

            lists.add(list);
            list = null;
        }
        return lists;
    }

    /**
     *
     * @return
     */
    @RequestMapping(value = "/getReceiveDetail/{type}")
    @ResponseBody
    public TableDataInfo getReceiveDetail(
            @PathVariable(value = "type") Integer type,
            Integer pageSize,
            Integer page,
            Long materiaId,
            String dateTime
    ) {
        try {
            PageHelper.startPage(page,pageSize);
            List<Materiel> list = reportFormService.getReceiveDetail(materiaId,type,dateTime);
            PageInfo pageInfo = new PageInfo(list,5);
            return getDataTable(pageInfo.getList(),Math.toIntExact(pageInfo.getTotal()));
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("出错");
            return getDataTable(new ArrayList<>(),100);
        }
    }

    /**
     * 总库存，支持多仓库
     * @param page 当前页
     * @param pageSize 每页大小
     * @param customer_id 客户id
     * @param maName 物料QAD
     * @param date 时间，（作废）
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getReportInventoryList")
    @ResponseBody
    public TableDataInfo getReportInventoryList(Integer page , Integer pageSize,Long customer_id,String maName,String locationCode) throws Exception {
        try {
            logger.info("获取总库存列表,page="+page+",pageSize="+pageSize+ ",ma_id="+maName+",locationCode="+locationCode);
            List<List<?>> results = reportFormService.getReportInventoryList(customer_id,maName,locationCode,page,pageSize);
            return getDataTable(results.get(1), (Integer) results.get(0).get(0));
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("获取总库存列表出错");
            return getDataTable(new ArrayList<>(),100);
        }
    }

    /**
     * 导出Excel
     * @param response
     * @throws UnsupportedEncodingException
     * @throws ParseException
     * @throws Exception
     */
    @RequestMapping("/doExportall")
    public void doExportall(HttpServletResponse response,Integer page , Integer pageSize,Long customer_id,String maName,String date) throws UnsupportedEncodingException, ParseException{
        page = 1;pageSize = 10000;
        List<List<?>> results = reportFormService.getReportInventoryList(customer_id,maName,date,page,pageSize);
        List<List<String>> lists = zhuanlistz((List<ZcInventoryInfoEntity>) results.get(1));
        ExcelUtils.export("总库存所有数据",namessz(), response, lists);
    }

    public List<String> namessz() {
        List<String> list = new ArrayList<>();
        list.add("序号");
        list.add("供应商");

        list.add("QAD");
        list.add("物料名称");
        list.add("实际数量");
        list.add("有码库存");
        list.add("无码库存");
        return list;
    }

    /**
     * 根据导出数据表头把List<cuser>转List<List<String>>
     * @param materiels
     * @return
     */
    public List<List<String>> zhuanlistz(List<ZcInventoryInfoEntity> zcRejectsWarehouseEntitys){
        int count = 0;
        List<List<String>> lists = new ArrayList<>();
        for (ZcInventoryInfoEntity zcRejectsWarehouse : zcRejectsWarehouseEntitys) {
            List<String> list = new ArrayList<>();
            ++count;
            list.add(String.valueOf(count));
            list.add(zcRejectsWarehouse.getmBatch());

            list.add(zcRejectsWarehouse.getMateriel().getMateriel_num());
            list.add(zcRejectsWarehouse.getMateriel().getMateriel_name());
            list.add(String.valueOf(zcRejectsWarehouse.getmNum()));
            int mnum=zcRejectsWarehouse.getmNum();
            int jnum=zcRejectsWarehouse.getjNum();
            if(mnum==0){
                list.add(String.valueOf(0));

            }else{
                list.add(String.valueOf(mnum-jnum));
            }
            list.add(String.valueOf(zcRejectsWarehouse.getjNum()));
            lists.add(list);
            list = null;
        }
        return lists;
    }


    /**
     * 总库存
     * @param model 
     * @param session
     * @return String
     */
    @RequestMapping("/pageTotalStock")
    public String pageTotalStock(Model model,HttpSession session){
        //查询所有的库位
        List<WarehouseRegionLocation> storeHouseList = reportFormService.getstoreHouse();
        //查询所有供应商
        List<Customer> allCustomerList = reportFormService.selectAllCustomerList();
        model.addAttribute("allCustomerList",allCustomerList);
        model.addAttribute("storeHouseList",storeHouseList);
        ZcSysUserEntity staf = (ZcSysUserEntity) session.getAttribute("user");
        if(staf.getRoleId().equals("20010,")) {
            Long customer_id = staf.getCustomer_id();
            model.addAttribute("customer_id",customer_id);
        }
        return "/bb/totalStock";
    }


    @RequestMapping("/getInventoryInfoList")
    @ResponseBody
    public TableDataInfo getInventoryInfoList(Integer page , Integer pageSize,String mid) {
        List<Materiel> list =  reportFormService.getInventoryInfoList(mid);
        //PageHelper.startPage(page,pageSize);
        //PageInfo pageInfo = new PageInfo(list,5);
        return getDataTable(list,100);
    }

    /**
     * 获取条码库存下表
     * @param mid 物料ID
     * @return
     */
    @RequestMapping("/getStockInventoryInfoDetailedList")
    @ResponseBody
    public TableDataInfo getStockInventoryInfoList(Integer page , Integer limit,String mBatch ,String mid) {
        try {
            logger.info("获取条码库存下表,page="+page+",pageSize="+limit+ ",mBatch="+mBatch+",mid="+mid);
            List<Materiel> list =  reportFormService.getStockInventoryInfoDetailedList(mid,mBatch,page,limit);
            Integer total = reportFormService.getStockInventoryInfoDetailedListTotal(mid,mBatch);
            return getDataTable(list,total);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(new ArrayList<>(),100);
        }
    }

    /**
     * 条码库存下表母子表内条码表
     * @param mBatch 批号
     * @param mid 物料ID
     * @param locationCode 库位
     * @return
     */
    @RequestMapping("/getTiaoMaList")
    @ResponseBody
    public TableDataInfo getTiaoMaList(
            String mBatch ,
            String mid,
            String locationCode,
            String time,
            String man
    ) {
        logger.info("获取条码库存下表母子表内条码表,locationCode="+locationCode+",mid="+mid);
        try {
            List<Materiel> list =  reportFormService.getTiaoMaList(mBatch,mid,locationCode);
            // for (Materiel mt : list) {
            //     mt.setBrevity_num(man);
            //     mt.setOut_date(time);
            // }
            return getDataTable(list,10000);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(new ArrayList<>(),100);
        }
    }

    @RequestMapping("queryAllCustomerByWareHouseList" )
    @ResponseBody
    public List<Customer> queryAllCustomerByWareHouseList(String wareHouseId) {
        //查询所有供应商
        List<Customer> allCustomerList = reportFormService.queryAllCustomerByWareHouseList(wareHouseId);
        return allCustomerList;
    }



}
